--[[
Copyright 2012
alatnet
--]]

dofile "extraFuncs.lc"
--dir /b
--lists files

local dircmd = "dir /B /A:-D-H-S-L"
local x32pso2Dir="C:\\Program Files\\SEGA\\PHANTASYSTARONLINE2"
local x64pso2Dir="C:\\Program Files (x86)\\SEGA\\PHANTASYSTARONLINE2"
local pso2DataDir="\\pso2_bin\\data\\win32"
local patchFiles="Patch Files"

local pso2Dir = ""

local timeTable = {}
timeTable = os.date("*t",os.time())
local pso2DirBackup = "Backups\\" .. timeTable["month"] .. "-" .. timeTable["day"] .. "-" .. timeTable["year"]

local filesToPatch = {}
--local pso2DirFiles = {}

--get the current directory
local cdf = io.popen("cd")
local currentDir = ""
if cdf then
  currentDir = cdf:read('*l')
  cdf:close()
else
  stdout("ERROR! Cannot execute cd command!\n")
  os.execute("pause")
  os.exit(false)
end
--print("Current Directory: " .. currentDir)

--get the list of files to patch
local dirf = io.popen(dircmd .. " \"" .. patchFiles .. "\"")
if dirf then
  for file in dirf:lines() do
    filesToPatch[#filesToPatch+1]=file
  end

  stdout("List of files to patch:\n")
  stdout(table.concat(filesToPatch, "\n"))
  stdout("\n")

  dirf:close()
else
  stdout("ERROR! Cannot execute dir command!\n")
  os.execute("pause")
  os.exit(false)
end

stdout("\nWhere is the PSO2 win32 data directory located?\n")
stdout("1. " .. x32pso2Dir .. "\n\t(32-bit OS, Default Install Directory)\n")
stdout("2. " .. x64pso2Dir .. "\n\t(64-bit OS, Default Install Directory)\n")
stdout("3. Other\n")
stdout(">")

local userInputDirChoice = tonumber(stdin())

if userInputDirChoice == 1 then
  pso2Dir = x32pso2Dir
elseif userInputDirChoice == 2 then
  pso2Dir = x64pso2Dir
elseif userInputDirChoice == 3 then
  --get location of directory
  stdout("\nEnter the location of the PSO2 directory\n    (i.e. C:\\Program Files\\SEGA\\PHANTASYSTARONLINE2):\n>")
  pso2Dir = stdin()
else
  os.exit(false)
end

stdout("\n\nPSO2 DIR: " .. pso2Dir .. "\n")
stdout("PSO2 BACKUP DIR: " .. pso2DirBackup .. "\n\n")

stdout("Is the PSO2 directory correct?\n")
stdout("1. Yes\n")
stdout("2. No\n")
stdout(">")
if not (tonumber(stdin()) == 1) then os.exit(true) end

stdout("\nCreating backup directory...")
os.execute("mkdir " .. pso2DirBackup)
stdout("OK!\n")

--copy or move files from pso2 directory to pso2DirBackup.
--(copy src1 + src2 .. dest [options]) (copy src1+src2 dest /B /V)
--(move [options] [source] [target])
stdout("\nBacking up files...\n")
for i=1,#filesToPatch do
  os.execute("copy \"" .. pso2Dir .. pso2DataDir .. "\\" .. filesToPatch[i] .. "\" \"" .. currentDir .. "\\" .. pso2DirBackup .. "\\" .. filesToPatch[i] .. "\" /B /V")
end

--[[
local copyFiles = table.concat(filesToPatch, "+")
local cmd = "copy \"" .. pso2Dir .. pso2DataDir .. "\\" .. copyFiles .. "\" \"" .. currentDir .. "\\" .. pso2DirBackup .. "\\\" /B /V"
print("\n" .. cmd)
os.execute(cmd)
--]]

stdout("OK!\n")

--delete files from pso2 directory.  (probably needs admin) (os.remove(filename))
stdout("\nRemoving files from PSO2 directory...")
for i=1,#filesToPatch do
  os.execute("del \"" .. pso2Dir .. pso2DataDir .. "\\" .. filesToPatch[i] .. "\"")
end
stdout("OK!\n")

--copy files from root of current folder to pso2 directory. (probably needs admin)
stdout("\nCopying patched files to PSO2 directory...\n")
for i=1,#filesToPatch do
  os.execute("copy \"" .. currentDir .. "\\" .. patchFiles .. "\\" .. filesToPatch[i] .. "\" \"" .. pso2Dir .. pso2DataDir .. "\\" .. filesToPatch[i] .. "\" /B /V")
end
stdout("OK!\n")

stdout("\nPatching is complete.\n")

os.execute("pause")